﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class ShowGotItem : MonoBehaviour { 
    /*public Camera mCamera;      //人物控制摄像机
    public Camera itemCamera;   //用于渲染道具和介绍的摄像机
    public Text Ifo;         //物品介绍文字
     
    Backpack bag;
    int itemIndex = 0;

    bool isLookItems;
    bool isLookItemBag;
    GameObject itemDisplay = null;
    GameObject itemTmp1 = null;
    GameObject itemDisplayTmp = null;
    Vector3 leftItemPos;
    Vector3 rightItemPos;


    Vector3 ItemPos2;
    Vector3 ItemPos;
    Rotate ItemRota;
    /// <summary>
    /// 按照输入值判断得到展示类型
    /// </summary>
    private enum ShowType     
    {
        First,
        Left,
        Right
    };
    /// <summary>
    /// 移动背包物品的状态
    /// </summary>
    private enum MoveType
    {
        left,
        right,
        Stay
    };
    //初始化此状态为停留状态
    MoveType moveType = MoveType.Stay;
    void Start()
    {
        leftItemPos = itemCamera.transform.position + new Vector3(-20f, 0, 10f);
        rightItemPos = itemCamera.transform.position + new Vector3(20f, 0, 10f);
        isCreated = false;
        isLookItems = false;
        isLookItemBag = false;
        bag = GetComponent<Backpack>();
    }
    void ShowFunc(GameObject aItem)
    {
        TaskItem taskItem = aItem.GetComponent<TaskItem>();
        ItemPos1 = itemCamera.transform.position + new Vector3(0, 0.63f, taskItem.cameraDistance); //调整物品展示的距离
        itemDisplay = Instantiate(aItem, ItemPos1, itemCamera.transform.rotation) as GameObject;
        itemDisplay.layer = 8;
        Ifo.text = taskItem.Intro;                                                   //获取物品特有介绍内容
        itemCamera.transform.parent.parent.gameObject.SetActive(true);              //item摄像机开启
        mCamera.GetComponentInChildren<GvrReticle>().gameObject.GetComponent<MeshRenderer>().enabled = true;
         mCamera.GetComponentInParent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().enabled = false;
        mCamera.GetComponentInParent<CharacterController>().enabled = false;
    }
    Vector3 ItemPos1;
    void ShowItemInstance(GameObject gotitem)
    {
        ShowFunc(gotitem);
        isLookItems = true;                                 //状态改为正在查看items
    }
    void ShowItems(ShowType theType)
    {

        if (theType == ShowType.First)
        {
			GameObject theItem = bag.getPrefab (bag [0]);
            ShowFunc(theItem);
            isLookItemBag = true;                                //状态改为正在查看itemBag
        }
        else if(theType == ShowType.Left)
        {
            itemDisplayTmp = itemDisplay;
			GameObject theItem2 = bag.getPrefab (bag [itemIndex]);
            TaskItem taskItem2 = theItem2.GetComponent<TaskItem>();
            Ifo.text = taskItem2.Intro;                                                   //获取物品特有介绍内容
            ItemPos = itemCamera.transform.position + new Vector3(0, 0.63f, taskItem2.cameraDistance);
            itemDisplay = Instantiate(theItem2, leftItemPos, itemCamera.transform.rotation) as GameObject;
            itemDisplay.layer = 8;
        //    ItemRota.target = itemDisplay;                          //给脚本的target赋为刚刚新建的物体
            moveType = MoveType.left;
            if (itemDisplayTmp != null && itemDisplay != null)
            {
                ItemMove.moveToPos(itemDisplayTmp, rightItemPos, 2f);
                ItemMove.moveToPos(itemDisplay, ItemPos);
            }

        }
        else if(theType == ShowType.Right)
        {
            itemDisplayTmp = itemDisplay;
			GameObject theItem2 = bag.getPrefab (bag [itemIndex]);
            TaskItem taskItem2 = theItem2.GetComponent<TaskItem>();
            Ifo.text = taskItem2.Intro;                                                   //获取物品特有介绍内容
            ItemPos = itemCamera.transform.position + new Vector3(0, 0.63f, taskItem2.cameraDistance);
            ItemPos2 = rightItemPos;
            itemDisplay = Instantiate(theItem2,ItemPos2, itemCamera.transform.rotation) as GameObject;
            itemDisplay.layer = 8;

            //  ItemRota.target = itemDisplay;                          //给脚本的target赋为刚刚新建的物体
            moveType = MoveType.right;
            if (itemDisplayTmp != null && itemDisplay != null)
            {
                ItemMove.moveToPos(itemDisplayTmp, leftItemPos, 2f);
                ItemMove.moveToPos(itemDisplay, ItemPos);
            }
        }
    }
    void HideFunc()
    {
        itemCamera.transform.parent.parent.gameObject.SetActive(false);

        mCamera.GetComponentInChildren<GvrReticle>().gameObject.GetComponent<MeshRenderer>().enabled = true;
        mCamera.GetComponentInParent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().enabled = true;
        mCamera.GetComponentInParent<CharacterController>().enabled = true;
        Destroy(itemDisplay);
    }
    void HideItemBag()
    {
        HideFunc();
        isLookItemBag = false;
        isCreated = false;
        itemIndex = 0;
    }

    void HideItem()
    {
        HideFunc();
        isLookItems = false;                                 //状态改为正在查看items
    }

    int itemTypeNum;   //获取背包物品的种类

    bool isCreated = false; //用于控制ShowItem每次触发只执行一次
    void Update()
    {
        Ray ray = mCamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            if (Input.GetMouseButtonDown(0))
            {
                TaskItem itemScr = hit.collider.gameObject.GetComponent<TaskItem>();
                if (itemScr != null && !isLookItems && !isLookItemBag) //如果该物体有TaskItem组件（是任务物品） 并且 此时状态为未查看item状态
                {
                    bag.AddItem(hit.collider.gameObject); //背包中加入得到的物品
                    ShowItemInstance(hit.collider.gameObject); //实时展示一次该物品
                }
            }
        }

        if (isLookItemBag) //如果查看背包
        {
            itemTypeNum = bag.Count; //得到背包中物品种类数量
            if (itemTypeNum == 1)   //如果只有一种物品
            {
                if (!isCreated)
                {
                    ShowItems(ShowType.First);
                    isCreated = true;
                }
            }
            if(itemTypeNum > 1)     //不止一种物品
            {
                if (!isCreated)
                {
                    ShowItems(ShowType.First);
                    isCreated = true;
                }
                if (Input.GetButtonDown("ShowLeft"))  //左滑
                {
                    itemIndex--;
                    if (itemIndex < 0)
                    {
                        itemIndex++;
                        return;
                    }
                    ShowItems(ShowType.Left);
                }
                if (Input.GetButtonDown("ShowRight")) //右滑
                {
                    itemIndex++;
                    if (itemIndex >= itemTypeNum)
                    {
                        itemIndex--;
                        return;
                    }
                    ShowItems(ShowType.Right);
                }      
            }
            if (Input.GetButtonDown("Confirm"))
            {
                HideItemBag();
            }
        }


        if (Input.GetButtonDown("ViewItems") && !isLookItems)
            isLookItemBag = true;

        if (isLookItems && !isLookItemBag)
        {
            if (Input.GetButtonDown("Confirm"))
            {
                HideItem();
            }
        }
    
    }
*/
}
